package dbinit;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import core.address.model.continent.Continent;
import core.address.model.country.Country;
import core.address.model.state.State;
import core.util.HibernateCoreUtil;

public class InitState 
{
	public static State getStateByName(String name)
	{
		State s = null ;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try
		{
			transaction = session.beginTransaction();
			s = (State)session.createCriteria(State.class).add(Restrictions.eq("stateName",name)).uniqueResult();
			transaction.commit();
		 }
		 catch(HibernateException e)
		 {
			 e.printStackTrace();
		 }
		 finally 
		{
				session.close();
		}
		return s ;
	}
	public static Long createOrGetUuidByName(Country country,String stateName,Continent continent,String abbreviation)
	{
		State so = null ;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Long uuid = 0L;
		try
		{
			transaction = session.beginTransaction();
			 List <State> sos = session.createQuery("from State c where c.stateName='"+stateName+"'").list();
			 if(sos.size() == 0)
			 {
				uuid = doinit(country, stateName ,continent ,abbreviation);
			 }
			 else
			 {
				 Iterator <State> sosItr = sos.iterator(); 
				 while(sosItr.hasNext())
				 {
					 so = sosItr.next() ;
					 break ;
				 }
				 so.setContinent(continent);
				 so.setCountry(country);
				 session.merge(so);
				 uuid = so.getUuid();
			 }
			 transaction.commit();
		}
		catch(HibernateException e)
		{
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return uuid;
	}
	
	public static State getStateByUuid(Long uuid)
	{
		State s = null ;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try 
		{
				transaction = session.beginTransaction();
				s = (State) session.get(State.class, uuid);
				transaction.commit();
		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return s ;
	}
	public static Long doinit(Country country,String stateName,Continent continent ,String abbreviation)
	{
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		State state = getStateByName(stateName);
		if(state == null)
		{
			state = new State();
		}
		state.setStateName(stateName);
		state.setContinent(continent);
		state.setCountry(country);
		state.setAbbreviation(abbreviation);
		Long uuid = null;
		try
		{
			transaction = session.beginTransaction();
			state = (State) session.merge(state);
		    uuid = state.getUuid();
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return uuid ;
	}
}
